package com.ronri.ams.sample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.cz.nongyetongv5.R;

import java.util.ArrayList;
import java.util.List;

import static com.cz.nongyetongv5.application.InitApplication.gSampleInfo;


/**
 * 文件：ams.java
 * 包名：com.ronri.ams.sample;
 * 说明：
 * 作者：MS During
 * 日期：2017/5/31
 * 历史：
 */
public class activity_region_select extends abstract_activity implements OnRegionSelected {

    private ViewPager mViewPager = null;

    private List<fragment_region> mFragmentList = new ArrayList<>();
    private List<Integer> mbLoad = new ArrayList<>();

    // 模块管理
    private FragmentManager fragmentManager;

    private int mCurrentIndex = 0;

    private TextView mTitle = null;
    private TextView mReset = null;

    private String mCurrentTitle = "";
    private String mCurrentCode = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region_select);
        Log.i("aaa","activity_region_select");
        setTitle("区域选择", "确定");

        init();

        mTitle = (TextView)findViewById(R.id.viewRegion);
        mReset = (TextView)findViewById(R.id.reset);

        // 设置点击事件
        mReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = 0;

                // 设置第一级
                if(getIntent().getBooleanExtra("add_entity",false)){
                    mFragmentList.get(0).setRegion("32");
                    mCurrentCode="32";
                    mCurrentTitle="江苏省";
                }else{
                    mCurrentCode = gSampleInfo.szDefaultRegionCode;
                    mFragmentList.get(0).setRegion(mCurrentCode);
                }


                setChioceItem(mCurrentIndex);
            }
        });

        fragmentManager = getSupportFragmentManager();

        // 1级
        mFragmentList.add(new fragment_region());
        // 2级
        mFragmentList.add(new fragment_region());
        // 3级
        mFragmentList.add(new fragment_region());
        // 4级
        mFragmentList.add(new fragment_region());
        // 5级
        mFragmentList.add(new fragment_region());

        mFragmentList.get(0).BindCallback(RefreshParentMb);
        mFragmentList.get(1).BindCallback(RefreshParentMb);
        mFragmentList.get(2).BindCallback(RefreshParentMb);
        mFragmentList.get(3).BindCallback(RefreshParentMb);
        mFragmentList.get(4).BindCallback(RefreshParentMb);

        // 设置第一级
        if(getIntent().getBooleanExtra("add_entity",false)){
            mFragmentList.get(0).setRegion("32");
            mCurrentCode="32";
            mCurrentTitle="江苏省";
        }else{
            mFragmentList.get(0).setRegion(gSampleInfo.getSample_area());

            mCurrentTitle = gSampleInfo.getArea_name();
            mCurrentCode = gSampleInfo.getSample_area();
        }

        setChioceItem(mCurrentIndex);
    }

    /**
     * 隐藏Fragment
     *
     * @param fragmentTransaction
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        for(int i = 0; i < mFragmentList.size(); i++) {
            mFragmentList.get(i).BindCallback(null);
            fragmentTransaction.hide(mFragmentList.get(i));
        }
    }

    public fragment_region.IRegionCallBack RefreshParentMb = new fragment_region.IRegionCallBack() {
        @Override
        public void CallBack(String szParentMb, String szParentCode) {
            mCurrentCode = szParentCode;
            mCurrentTitle = szParentMb;
            mTitle.setText(szParentMb);
        }
    };

    /***
     * 激活页面
     * @param index
     */
    private void setChioceItem(int index) {
        String szMianBao ="";
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        // 设置动画效果
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        // 首先隐藏所有的Fragment页面
        hideFragments(fragmentTransaction);

        // 取fragment
        fragment_region fragment = mFragmentList.get(index);
        fragment.BindCallback(RefreshParentMb);
        if (mbLoad.contains(index)){
            fragmentTransaction.show(fragment);
        }else{
            mbLoad.add(index);
            fragmentTransaction.add(R.id.content, fragment);
            fragmentTransaction.show(fragment);
        }

        /*String szSplit = "";
        for(int i = 0; i < index;i++){
            szMianBao += szSplit + mFragmentList.get(i).getParentName();
            szSplit = ">>";
        }
        szMianBao = "区域：" + szMianBao;*/
        mTitle.setText(szMianBao);

        fragmentTransaction.commit(); // 提交
    }

    @Override
    public void RegionSelected(String szTitle, String szRegionCode) {

        if (mCurrentIndex < mFragmentList.size()) {
            mFragmentList.get(mCurrentIndex).setParentName(szTitle);
            mCurrentIndex++;
            fragment_region fragment = mFragmentList.get(mCurrentIndex);
            // fragment.setParentName(szTitle);
            fragment.setRegion(szRegionCode);

            mCurrentTitle = szTitle;
            mCurrentCode = szRegionCode;

            setChioceItem(mCurrentIndex);
        }
    }

    @Override
    public boolean CancelReturn() {
        if (mCurrentIndex > 0)
            setChioceItem(--mCurrentIndex);
        else {
            setResult(RESULT_CANCELED);
            return true;
        }

        return false;
    }

    @Override
    public boolean SureReturn() {

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("region_name", mCurrentTitle);
        bundle.putString("region_code", mCurrentCode);

        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);

        return super.SureReturn();
    }
}
